package edu.info.manager.controller;


import edu.info.manager.domain.TeacherJavaBean;
import edu.info.manager.service.TeacherService;

import java.util.Scanner;

public class TeacherController {
    Scanner scanner = new Scanner(System.in);
    TeacherService tea = new TeacherService();

    public void TeacherStart() {
        log:
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            int choose = scanner.nextInt();
            switch (choose) {
                case 1:

                    AddTeacher();
                    break;

                case 2:
//                    System.out.println("删除老师");
                    DelTeacher();
                    break;
                case 3:
                    UpdateTeacher();
                    break;
                case 4:
//                    System.out.println("查看老师");
                    FindAllTeacher();
                    break;
                case 5:
                    System.out.println("退出");
                    break log;
                default:
                    System.out.println("输出有误，请重新输入.");
                    break;

            }
        }

    }

    private void DelTeacher() {
        String Delnum;
        while (true){

            System.out.println("请输入你要删除的Id：");
            Delnum = scanner.next();
            boolean flag = tea.isExists(Delnum);
            if(!flag)
            {
                System.out.println("你输入的ID不存在，请重新输入");

            }else
            {
                break;
            }
        }
        tea.DeleteTeacher(Delnum);
        System.out.println("删除成功！");
    }

    private void FindAllTeacher() {
        String Findid;
        TeacherJavaBean teacherJavaBean[] =TeacherService.FindAllTeacher();
        if(teacherJavaBean==null)
        {
            System.out.println("查无信息，请添加输入");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < teacherJavaBean.length; i++) {
            TeacherJavaBean tea=teacherJavaBean[i];
            if (tea!=null)
            {
                System.out.println(tea.getId() + "\t" + tea.getName() + "\t" + tea.getAge() + "\t\t" + tea.getBirthday());
            }
        }
    }

    private void UpdateTeacher() {
        String updateid;
        while (true) {
            System.out.println("请输入老师ID：");
            updateid = scanner.next();
//            System.out.println(updateid+"=============");
            boolean flag = tea.isExists(updateid);
//            System.out.println(flag+"-----------");
            if (!flag) {
                System.out.println("你输入的ID不存在。");
            } else {
                break;
            }
        }
        System.out.println("请输入老师的姓名");
        String name = scanner.next();
        System.out.println("请输入老师的年龄");
        String age = scanner.next();
        System.out.println("请输入老师的生日");
        String birthday = scanner.next();
        TeacherJavaBean newstu = new TeacherJavaBean();
        newstu.setId(updateid);
        newstu.setAge(age);
        newstu.setName(name);
        newstu.setBirthday(birthday);
        boolean flag = tea.UpdateTeachher(updateid, newstu);
        if (true) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }

    }

    private void AddTeacher() {
        String id;
        while (true) {
            System.out.println("请输入老师ID：");
            id = scanner.next();
            boolean flag = tea.isExists(id);
            if (flag) {
                System.out.println("老师ID已被占用");
            } else
                break;
        }
        System.out.println("请输入老师名字：");
        String name = scanner.next();
        System.out.println("请输入老师的年龄：");
        String age = scanner.next();
        System.out.println("请输入老师的生日：");
        String birthday = scanner.next();
        TeacherJavaBean Tea = new TeacherJavaBean();
        Tea.setId(id);
        Tea.setAge(age);
        Tea.setName(name);
        Tea.setBirthday(birthday);

        boolean result = tea.AddTeacher(Tea);
        if (result) {
            System.out.println("添加成功");
        } else
            System.out.println("添加失败");
    }
}
